//
//  ZBGiftOperation.m
//  送礼
//
//  Created by 华惠友 on 2017/9/20.
//  Copyright © 2017年 华惠友. All rights reserved.
//

#import "ZBGiftOperation.h"

// 各类动画
#import "ZBPathAnimationView.h"
#import "ZBFireAnimationView.h"
#import "ZBKeyFrameAnimationView.h"
#import "ZBGravityAnimationView.h"
#import "ZBCarAnimationView.h"

@interface ZBGiftOperation ()


/**
 重写父类属性
 */
//@property (readonly, getter=isCancelled) BOOL cancelled;
@property (readonly, getter=isExecuting) BOOL executing;
@property (readonly, getter=isFinished) BOOL finished;
//@property (readonly, getter=isConcurrent) BOOL concurrent;
//@property (readonly, getter=isAsynchronous) BOOL asynchronous;
//@property (readonly, getter=isReady) BOOL ready;


@end

@implementation ZBGiftOperation

@synthesize finished = _finished;
@synthesize executing = _executing;

+ (instancetype)animationWithGiftIndex:(NSInteger)giftIndex
                             giftModel:(ZBGiftModel *)giftModel
                         finishedBlock:(GiftFinishedBlock)finishedBlock{
    ZBGiftOperation *op = [[ZBGiftOperation alloc] init];
    // 初始化方法里完成标签的初始化视图, 便于后期连续送礼重用
    op.presentView = [[ZBPresentView alloc] init];
    op.giftModel = giftModel;
    op.finishedBlock = finishedBlock;
    op.giftIndex = giftIndex;
    return op;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _executing = NO;
        _finished = NO;
    }
    return self;
}

// 重写父类方法,  只有将队列主动start, 或者加入到队列中, 才会执行该方法
- (void)start {
    if ([self isCancelled]) {
        self.finished = YES;
        return;
    }
    
    // 动画开始了
    self.executing = YES;
    
    // 主线程执行动画
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self labelAnimation];
        [self viewAnimation];
    }];
}

- (void)labelAnimation {
    // 振动标签的动画presentVeiw
    _presentView.model = self.giftModel;
    _presentView.originFrame = _presentView.frame;
    [self.parentView addSubview:_presentView];

    [_presentView animateWithCompleteBlock:^(BOOL finished, NSInteger giftCount) {
        self.finished = YES;
        self.executing = NO;
        self.finishedBlock(finished, giftCount);
    }];
}

- (void)viewAnimation {
    // 根据tag 来做不同的特效动画
    switch (self.giftIndex) {
        case 1000: { // 轨迹轮船
            ZBPathAnimationView *pathView = [ZBPathAnimationView sharedInstance]; // [[ZBPathAnimationView alloc] init];
            if (pathView.isAnimating) { // 表示正在执行动画  跳过 不再做动画  否则做动画
                return;
            }
            [pathView animationWithPath:^{
//                self.finished = YES;
//                self.finishedBlock(YES, 1);
            }];
            [self.parentView insertSubview:pathView atIndex:1];
//            [self.parentView addSubview:pathView];
        }
            break;
        case 1001: { // 粒子花花
            ZBFireAnimationView *fireAnimation = [[ZBFireAnimationView alloc] init];
            [fireAnimation animationWithFire:^{
//                self.finishedBlock(YES, 1);
            }];
            [self.parentView insertSubview:fireAnimation atIndex:1];

//            [self.parentView addSubview:fireAnimation];
        }
            break;
        case 1002: { // 帧动画钻石
            ZBKeyFrameAnimationView *frameAnimation = [ZBKeyFrameAnimationView sharedInstance];// [[ZBKeyFrameAnimationView alloc] init];
            if (frameAnimation.isAnimating) { // 表示正在执行动画  跳过 不再做动画  否则做动画
                return;
            }
            [frameAnimation animationWithKeyFrame:^{
//                self.finishedBlock(YES, 1);
            }];
            [self.parentView insertSubview:frameAnimation atIndex:1];
//            [self.parentView addSubview:frameAnimation];
        }
            break;
        case 1003: { // 重力❤️
            ZBGravityAnimationView *gravityAnimation = [[ZBGravityAnimationView alloc] init];
            [gravityAnimation animationWithGravity:^{
//                self.finishedBlock(YES, 1);
            }];
            [self.parentView insertSubview:gravityAnimation atIndex:1];

//            [self.parentView addSubview:gravityAnimation];
        }
            break;
        case 1004: { // UIView动画 法拉利
            ZBCarAnimationView *carAnimation = [ZBCarAnimationView sharedInstance];// [[ZBCarAnimationView alloc] init];
            if (carAnimation.isAnimating) { // 表示正在执行动画  跳过 不再做动画  否则做动画
                return;
            }
            [carAnimation animationWithCar:^{
//                self.finishedBlock(YES, 1);
                
            }];
            [self.parentView insertSubview:carAnimation atIndex:1];

//            [self.parentView addSubview:carAnimation];
        }
            break;
            
        default: { // 其他的没有特效的
//            self.finishedBlock(YES, 1);
        }
            break;
    }

}


#pragma mark - 手动触发队列状态KVO
- (void)setExecuting:(BOOL)executing
{
    [self willChangeValueForKey:@"isExecuting"];
    _executing = executing;
    [self didChangeValueForKey:@"isExecuting"];
}

- (void)setFinished:(BOOL)finished
{
    [self willChangeValueForKey:@"isFinished"];
    _finished = finished;
    [self didChangeValueForKey:@"isFinished"];
}















@end
